package com.mes.web.controller.hy;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.mes.entity.hy.PeopleManage;
import com.mes.service.hy.IPeopleManageService;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;

/**
 * 
 * @ClassName:PeopleManageController
 * @Description: 人员管理controller层
 * @Author hy
 * @Date 2018-5-30下午3:39:49
 * @version 1.0
 */
@Controller
@RequestMapping("/peopleManage")
public class PeopleManageController {
	
	@Resource
	private IPeopleManageService iPeopleManageService;

	/**
	 * 查询人员工作情况
	 * @param response
	 * @param limit 每页显示行数
	 * @param offset 页码
	 * @param peopleManage 人员管理实体类
	 * @throws Exception
	 */
	@RequestMapping("/select")
	public void select(HttpServletResponse response,
			@RequestParam(value = "limit", required = false) String limit,
			@RequestParam(value = "offset", required = false) String offset,
			PeopleManage peopleManage) throws Exception {
		//转码
		peopleManage.setPeoplenum(new String(peopleManage.getPeoplenum().trim()
				.getBytes("ISO-8859-1"), "utf-8"));
		peopleManage.setWorkstate(new String(peopleManage.getWorkstate().trim()
				.getBytes("ISO-8859-1"), "utf-8"));
		
		if (!"".equals(offset)) {
			peopleManage.setPage(Integer.parseInt(offset) / Integer.parseInt(limit));
		}
		peopleManage.setPageSize(Integer.parseInt(limit));
		//转码
		if(!("").equals(peopleManage.getWorkstate()) && peopleManage.getWorkstate()
				.equals(new String(peopleManage.getWorkstate().getBytes("iso-8859-1"),"utf-8"))){
			peopleManage.setWorkstate(new String(peopleManage.getWorkstate()
					.getBytes("iso-8859-1"),"utf-8"));
		}
		if(!("").equals(peopleManage.getPeoplenum()) && peopleManage.getPeoplenum()
				.equals(new String(peopleManage.getPeoplenum().getBytes("iso-8859-1"),"utf-8"))){
			peopleManage.setPeoplenum(new String(peopleManage.getPeoplenum()
					.getBytes("iso-8859-1"),"utf-8"));
		}
		
		List<PeopleManage> peopleList = iPeopleManageService.select(peopleManage);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date st,et;//date类型的开始时间和结束时间
		Long tt;//时间差
		Double time;//小时差
		for(int i = 0;i < peopleList.size();i++){
			st = sdf.parse(peopleList.get(i).getStarttime());
			et = sdf.parse(peopleList.get(i).getEndtime());
			tt = et.getTime() - st.getTime();
			time = ((double)tt / (60*60*1000));
			peopleList.get(i).setWorktimelong(String.format("%.1f", time));
		}
		int total = iPeopleManageService.selectCount(peopleManage);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,
				new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		JSONArray jsonArray = JSONArray.fromObject(peopleList, jsonConfig);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
}
